//var mousePos;
var xmlTransHttp = null;
var translateDiv = document.createElement("div");
translateDiv.setAttribute("id", "translateDiv");
//translateDiv.innerHTML = "fjwoerj23j92 3u42893u";
var stylesheet = document.createElement("style");
translateDiv.appendChild(stylesheet);
stylesheet.innerHTML = "#translateDiv{	position: fixed;	top: 0px;	right: 0px;width:300px;padding:20px;border-radius: 12px; -webkit-box-shadow: 0px 0px 4px #ffffff;	background-color: #ffffcc;box-shadow: 0px 0px 4px #ffffff;background-color: #444444;background-image: -webkit-gradient(linear,left top,left bottom,color-stop(0, #444444),color-stop(1, #999999));text-shadow: 1px 1px 3px #888;font-family: 'WebFont';src:  url('myfont.ttf') format('truetype');}";
var contentTable = document.createElement("table");
translateDiv.appendChild(contentTable);
contentTable.setAttribute("id", "contentTable");
//translateDiv.style.position = "absolute";
//translateDiv.style.top = "200px";
//translateDiv.style.left = "300px";
//translateDiv.style.backgroundColor = "#ffffcc";
translateDiv.style.display = "none";

document.body.appendChild(translateDiv);

chrome.extension.onRequest.addListener(
  function(request, sender, sendResponse) {
      if (request.type == "getselection") {
          var selection = window.getSelection();
          var txt = window.getSelection().toString();
          var url = "https://www.googleapis.com/language/translate/v2?key=" + "AIzaSyD3_KyaIis7pklJsNXt_isG7QzkTYPmf2w" + "&q=" + txt + "&target=";
          popupTranslation("Loading...");
          sendResponse({ url: url });
      }
      else {
          popupTranslation(request.txt);
      }
  });

  function closeTranslateText() {
      alert("called");
//      var my = document.getElementById("translateTxtWindow");
//      document.body.removeChild(my);
  }


  function popupTranslation(txt) {

//      var my = document.createElement("div");
//      var tmpAttr = document.createAttribute("id")
//      tmpAttr.value = "translateTxtWindow";
//      my.attributes.setNamedItem(tmpAttr);
      //my.attributes.setNamedItem("id", "translateTxtWindow");
      //      document.body.appendChild(my);
//      var my = document.getElementById("translateDiv");
//      my.style.position = "absolute";
////      my.style.top = mousePos.y + "px";
//      //      my.style.left = mousePos.x + "px";
//      my.style.top = "200px";
//      my.style.left = "300px";
//      my.style.backgroundColor = "#ffffcc";
      //      my.innerHTML = txt;
      contentTable.innerHTML= "<B>ClickTrans result:</B><br/>" + txt + "<br/>";
      translateDiv.style.display = "";
   }
  
//  function mousePosition(ev){ 
//    if(ev.pageX || ev.pageY){ 
//        return {x:ev.pageX, y:ev.pageY}; 
//    } 
//    return { 
//        x:ev.clientX + document.body.scrollLeft - document.body.clientLeft, 
//        y:ev.clientY + document.body.scrollTop  - document.body.clientTop 
//    }; 
//}

//document.onmousemove = mouseMove;
document.body.onmouseup = mouseUp;

//function mouseMove(ev) {
//    ev = ev || window.event;
//    mousePos = mousePosition(ev);
//}

function mouseUp(ev) {
    ev = ev || window.event;
    if (!translateDiv.contains(window.event.srcElement)) {
        translateDiv.style.display = "none";
    }
}

function getElementPos(elementId) {

    var ua = navigator.userAgent.toLowerCase();
    var isOpera = (ua.indexOf('opera') != -1);
    var isIE = (ua.indexOf('msie') != -1 && !isOpera); // not opera spoof 

    var el = document.getElementById(elementId);

    if (el.parentNode === null || el.style.display == 'none') {
        return false;
    }

    var parent = null;
    var pos = [];
    var box;

    if (el.getBoundingClientRect)    //IE 
    {
        box = el.getBoundingClientRect();
        var scrollTop = Math.max(document.documentElement.scrollTop, document.body.scrollTop);
        var scrollLeft = Math.max(document.documentElement.scrollLeft, document.body.scrollLeft);

        return { x: box.left + scrollLeft, y: box.top + scrollTop };
    }
    else if (document.getBoxObjectFor)    // gecko 
    {
        box = document.getBoxObjectFor(el);

        var borderLeft = (el.style.borderLeftWidth) ? parseInt(el.style.borderLeftWidth) : 0;
        var borderTop = (el.style.borderTopWidth) ? parseInt(el.style.borderTopWidth) : 0;

        pos = [box.x - borderLeft, box.y - borderTop];
    }
    else    // safari & opera 
    {
        pos = [el.offsetLeft, el.offsetTop];
        parent = el.offsetParent;
        if (parent != el) {
            while (parent) {
                pos[0] += parent.offsetLeft;
                pos[1] += parent.offsetTop;
                parent = parent.offsetParent;
            }
        }
        if (ua.indexOf('opera') != -1
            || (ua.indexOf('safari') != -1 && el.style.position == 'absolute')) {
            pos[0] -= document.body.offsetLeft;
            pos[1] -= document.body.offsetTop;
        }
    }

    if (el.parentNode) { parent = el.parentNode; }
    else { parent = null; }

    while (parent && parent.tagName != 'BODY' && parent.tagName != 'HTML') { // account for any scrolled ancestors 
        pos[0] -= parent.scrollLeft;
        pos[1] -= parent.scrollTop;

        if (parent.parentNode) { parent = parent.parentNode; }
        else { parent = null; }
    }
    return { x: pos[0], y: pos[1] };
}

//--> 

